n = int(input())
for i in range(n):
a = int(input())
b = a//10
if b == 0:
print(a)
else:
c = len(str(b))*9
d = a//int(int(len(str(a)) * '1'))
print(d+c)
#include <bits/stdc++.h>
#define ll long long
#define db double
#define str string
#define vi vector<int>
#define vll vector<ll>
#define maax(a,b,c) max(max(a,b),c)
#define diff(a,b) abs(a-b)
#define pi 2*asin(1)
#define pb push_back
#define mp make_pair
#define sfi(a) scanf("%d", &a)
#define sfl(a) scanf("%lld", &a)
#define sfc(a) scanf("%ch",&a)
#define pf printf
#define pii pair<int, int>
#define all(x) x.begin(),x.end()
#define iobost ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define el '\n'
#define sp ' '
//318424JO timus judge id
using namespace std;
int main()
{
iobost;
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int ans=0;
for(int power=1;power<=n;power=power*10+1)
{
for(int d=1;d<10;d++)
{
if(d*power<=n)ans++;
}
}
cout<<ans<<el;
}
return 0;
}
1452A - Robot Program | 344A - Magnets |
96A - Football | 702B - Powers of Two |
1036A - Function Height | 443A - Anton and Letters |
1478B - Nezzar and Lucky Number | 228A - Is your horseshoe on the other hoof |
122A - Lucky Division | 1611C - Polycarp Recovers the Permutation |
432A - Choosing Teams | 758A - Holiday Of Equality |
1650C - Weight of the System of Nested Segments | 1097A - Gennady and a Card Game |
248A - Cupboards | 1641A - Great Sequence |
1537A - Arithmetic Array | 1370A - Maximum GCD |
149A - Business trip | 34A - Reconnaissance 2 |
59A - Word | 462B - Appleman and Card Game |
1560C - Infinity Table | 1605C - Dominant Character |
1399A - Remove Smallest | 208A - Dubstep |
1581A - CQXYM Count Permutations | 337A - Puzzles |
495A - Digital Counter | 796A - Buying A House |